﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FaceCat;
using OpenTK;
using SkiaSharp;
using SkiaSharp.Views;
using SkiaSharp.Views.Desktop;

namespace FaceCat
{
    /// <summary>
    /// 自定义组件
    /// </summary>
    public class FCUIView : SKControl
    {
        /// <summary>
        /// 公共方法库
        /// </summary>
        public FCNative m_native;

        /// <summary>
        /// 设备对接
        /// </summary>
        public WinHost m_host;

        /// <summary>
        /// 配置解析
        /// </summary>
        private FCUIXml m_xml;

        /// <summary>
        /// Skia绘图
        /// </summary>
        public SkiaPaint m_paint;

        /// <summary>
        /// 初始化视图
        /// </summary>
        /// <param name="xml">配置解析</param>
        /// <param name="path">路径</param>
        public void initView(FCUIXml xml, String path)
        {
            if (xml != null)
            {
                m_xml = xml;
                m_xml.createNative();
                m_native = m_xml.getNative();
                m_paint = new SkiaPaint();
                m_native.setPaint(m_paint);
                m_host = new WinHost();
                m_host.setNative(m_native);
                m_native.setHost(m_host);
                m_host.setHWnd(Handle);
                FCSize clientSize = new FCSize(ClientSize.Width, ClientSize.Height);
                m_native.setSize(new FCSize(clientSize.cx, clientSize.cy));
                m_native.setScaleSize(new FCSize((int)(clientSize.cx * m_scaleFactor), (int)(clientSize.cy * m_scaleFactor)));
                m_native.setAllowScaleSize(true);
                m_xml.loadFile(path, null);
                
                m_native.update();
            }
            this.PaintSurface += FCUIView_PaintSurface;
            this.Disposed += FCUIView_Disposed;
        }

        /// <summary>
        /// 缩放比例
        /// </summary>
        public double m_scaleFactor = 0.5;

        /// <summary>
        /// 尺寸改变方法
        /// </summary>
        /// <param name="e">参数</param>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (m_native != null)
            {
                FCSize clientSize = new FCSize(ClientSize.Width, ClientSize.Height);
                m_native.setSize(clientSize);
                m_native.setScaleSize(new FCSize((int)(clientSize.cx * m_scaleFactor), (int)(clientSize.cy * m_scaleFactor)));
                m_native.update();
                m_native.invalidate();
            }
        }

        /// <summary>
        /// 消息监听
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            try
            {
                if (m_host != null)
                {
                    if (m_host.onMessage(ref m) > 0)
                    {
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
            }
            base.WndProc(ref m);
        }

        /// <summary>
        /// 销毁组件
        /// </summary>
        /// <param name="sender">调用者</param>
        /// <param name="e">参数</param>
        private void FCUIView_Disposed(object sender, EventArgs e)
        {
            if (this.m_native != null)
            {
                this.m_native.delete();
                this.m_native = null;
                System.GC.Collect();
            }
        }

        public int m_drawTimes;

        /// <summary>
        /// 重绘方法
        /// </summary>
        /// <param name="sender">调用者</param>
        /// <param name="e">参数</param>
        private void FCUIView_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            m_drawTimes++;
            if (m_drawTimes > 10000)
            {
                m_drawTimes = 0;
            }
            var canvas = e.Surface.Canvas;
            m_paint.m_canvas = canvas;
            FCSize displaySize = m_native.getSize();
            FCSize clientSize = new FCSize(ClientSize.Width, ClientSize.Height);
            if (displaySize.cx > 0 && displaySize.cy > 0 && clientSize.cx > 0 && clientSize.cy > 0)
            {
                double scaleFactorX = 1, scaleFactorY = 1;
                if (m_native.allowScaleSize())
                {
                    if (clientSize.cx > 0 && clientSize.cy > 0)
                    {
                        FCSize scaleSize = m_native.getScaleSize();
                        scaleFactorX = (double)(clientSize.cx) / scaleSize.cx;
                        scaleFactorY = (double)(clientSize.cy) / scaleSize.cy;
                    }
                }
                FCRect clipRect = new FCRect();
                int clipBoundsSize = m_host.m_clipBounds.size();
                if (clipBoundsSize > 0 && m_drawTimes > 10)
                {
                    for (int i = 0; i < clipBoundsSize; i++)
                    {
                        FCRect clipBounds = m_host.m_clipBounds.get(i);
                        if (i == 0)
                        {
                            clipRect = new FCRect(clipBounds.left, clipBounds.top, clipBounds.right, clipBounds.bottom);
                        }
                        else
                        {
                            if (clipRect.left > clipBounds.left)
                            {
                                clipRect.left = clipBounds.left;
                            }
                            if (clipRect.right < clipBounds.right)
                            {
                                clipRect.right = clipBounds.right;
                            }
                            if (clipRect.top > clipBounds.top)
                            {
                                clipRect.top = clipBounds.top;
                            }
                            if (clipRect.bottom < clipBounds.bottom)
                            {
                                clipRect.bottom = clipBounds.bottom;
                            }
                        }
                    }
                }
                else
                {
                    clipRect = new FCRect(0, 0, displaySize.cx, displaySize.cy);
                }
                m_host.m_clipBounds.clear();
                m_native.getPaint().setScaleFactor(scaleFactorX, scaleFactorY);
                m_native.getPaint().beginPaint(IntPtr.Zero, new FCRect(0, 0, clientSize.cx, clientSize.cy), clipRect);
                m_native.onPaint(clipRect);
                m_native.getPaint().endPaint();
            }
        }
    }
}
